using Game.Component.Character;
using Game.Core;
using Game.Main.Model;
using Game.Manager;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class CupItem : BaseItem
	{
		public enum State
		{
			Static = 0,
			AddFill = 1,
			MinusFill = 2
		}

		public float fillAmount;

		private State state;

		protected SpriteRenderer waterSprite;

		private ItemComponentTrigger targetItem;

		protected Color32 mixColor;

		protected float fillTimer;

		protected bool needMixColor;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			InitWater(data);
		}

		protected virtual void InitWater(ItemArchiveData data)
		{
			waterSprite = model.Find("WaterFill").GetComponent<SpriteRenderer>();
			float num = waterSprite.sprite.texture.height;
			Rect textureRect = waterSprite.sprite.textureRect;
			float num2 = textureRect.yMin / num;
			float value = textureRect.yMax / num - num2;
			waterSprite.material.SetFloat("_StartY", num2);
			waterSprite.material.SetFloat("_TotalY", value);
			waterSprite.color = data.bodyColor;
			SetFillAmount(data.floatParam1);
			needMixColor = true;
		}

		protected override void LateUpdate()
		{
			base.LateUpdate();
			LatedUpdateFill();
		}

		protected virtual void LatedUpdateFill()
		{
			if (state == State.Static)
			{
				return;
			}
			if (state == State.AddFill)
			{
				AddFill(Time.deltaTime / 2f);
				fillTimer += Time.deltaTime / 2f;
				if (needMixColor)
				{
					UpdateMixColor(mixColor);
				}
			}
			else if (state == State.MinusFill)
			{
				MinusFill(Time.deltaTime / 2f);
				if (fillAmount <= 0f)
				{
					RecoverNormal();
					RemoveAudioPlayer();
				}
			}
		}

		protected override void OnItemCompTriggerEnter(ColliderData collData)
		{
			base.OnItemCompTriggerEnter(collData);
			if (collData.trigger.type == ItemComponentTrigger.Type.WaterOutlet)
			{
				mixColor = collData.trigger.owner.GetBodyColor();
				fillTimer = 0f;
				state = State.AddFill;
				RemoveOldTarget();
				targetItem = collData.trigger;
				if (targetItem != null && IsWaterDispenser(targetItem.owner))
				{
					((WaterDispenserItem)targetItem.owner).AddTargetItem(this);
				}
				Singleton<AudioManager>.Instance.PlayAudio("sound_fruit_machine", true, GetAudioPlayer());
			}
			else if (collData.trigger.type == ItemComponentTrigger.Type.Mouth && fillAmount > 0f)
			{
				state = State.MinusFill;
				RemoveOldTarget();
				targetItem = collData.trigger;
				if (typeof(CharacterItem).IsAssignableFrom(targetItem.owner.GetType()))
				{
					((CharacterItem)targetItem.owner).SetMouthState(Face.MouthState.Open);
				}
				RotateToAngle(30f);
				Singleton<AudioManager>.Instance.PlayAudio("sound_drinking", true, GetAudioPlayer());
			}
		}

		private void RemoveOldTarget()
		{
			if (targetItem != null && IsWaterDispenser(targetItem.owner))
			{
				((WaterDispenserItem)targetItem.owner).RemoveTargetItem(this);
			}
		}

		public void UpdateMixColor(Color32 color)
		{
			if (needMixColor)
			{
				waterSprite.color = MixColor(waterSprite.color, color, fillAmount, Time.deltaTime);
			}
		}

		private bool IsWaterDispenser(BaseItem item)
		{
			if (item != null && (item.itemData.scriptType == 37 || item.itemData.scriptType == 131))
			{
				return true;
			}
			return false;
		}

		protected override void OnItemCompTriggerExit(ColliderData collData)
		{
			base.OnItemCompTriggerExit(collData);
			if (collData.trigger == targetItem)
			{
				RecoverNormal();
			}
		}

		public override void TouchUp(TouchEventData touchEventData)
		{
			base.TouchUp(touchEventData);
			RecoverNormal();
		}

		private void RecoverNormal()
		{
			state = State.Static;
			if (targetItem != null)
			{
				if (IsWaterDispenser(targetItem.owner))
				{
					((WaterDispenserItem)targetItem.owner).RemoveTargetItem(this);
				}
				else if (typeof(CharacterItem).IsAssignableFrom(targetItem.owner.GetType()))
				{
					((CharacterItem)targetItem.owner).SetMouthState(Face.MouthState.Normal);
					RotateToAngle(0f);
				}
				targetItem = null;
				RemoveAudioPlayer();
			}
		}

		protected Color32 MixColor(Color32 color1, Color32 color2, float fill1, float fill2)
		{
			float num = Mathf.Clamp(fill1 / (fill1 + fill2), 0f, 0.97f);
			float num2 = Mathf.Clamp(fill2 / (fill1 + fill2), 0.03f, 1f);
			return new Color32((byte)((float)(int)color1.r * num + (float)(int)color2.r * num2), (byte)((float)(int)color1.g * num + (float)(int)color2.g * num2), (byte)((float)(int)color1.b * num + (float)(int)color2.b * num2), byte.MaxValue);
		}

		public void AddFill(float val)
		{
			if (fillAmount < 1f)
			{
				fillAmount += val;
				if (fillAmount >= 1f)
				{
					fillAmount = 1f;
				}
				SetFillAmount(fillAmount);
			}
		}

		public bool isFull()
		{
			if (fillAmount >= 1f)
			{
				return true;
			}
			return false;
		}

		public void MinusFill(float val)
		{
			if (fillAmount > 0f)
			{
				fillAmount -= val;
				if (fillAmount <= 0f)
				{
					fillAmount = 0f;
				}
				SetFillAmount(fillAmount);
			}
		}

		protected void SetFillAmount(float val)
		{
			fillAmount = val;
			waterSprite.material.SetFloat("_Fill", fillAmount);
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
			PackDetailData(obj);
		}

		protected virtual void PackDetailData(JSONObject obj)
		{
			obj["62"] = fillAmount;
			obj["27"] = ((Color32)waterSprite.color).r;
			obj["28"] = ((Color32)waterSprite.color).g;
			obj["29"] = ((Color32)waterSprite.color).b;
		}
	}
}
